﻿using System;

class Matrixes
{
    static void Main()
    {
        Console.Write("Enter the size of the matrix: ");

        int size = int.Parse(Console.ReadLine());

        // 1. a)
        int[,] matrixA = new int[size, size];

        CalculateMatrixA(size, matrixA);

        PrintMatrix(size, matrixA);

        // 1. b)
        int[,] matrixB = new int[size, size];

        CalculateMatrixB(size, matrixB);

        PrintMatrix(size, matrixB);

        // 1. c)
        int[,] matrixC = new int[size, size];

        CalculateMatrixC(size, matrixC);

        PrintMatrix(size, matrixC);

        // 1. d)*
        int[,] matrixD = new int[size, size];

        CalculateMatrixD(size, matrixD);

        PrintMatrix(size, matrixD);
    }

    private static void CalculateMatrixD(int size, int[,] matrixD)
    {
        int counter = 1;
        int top = 0;
        int bottom = 0;
        int left = 0;
        int right = 0;
        string nextDirection = "down";

        while (counter <= size * size)
        {
            if (nextDirection == "down")
            {
                for (int row = top, col = left; row < size - bottom; row++)
                {
                    matrixD[row, col] = counter++;
                }

                left++;
                nextDirection = "right";
            }
            else if (nextDirection == "right")
            {
                for (int row = (size - 1) - bottom, col = left; col < (size - right); col++)
                {
                    matrixD[row, col] = counter++;
                }

                bottom++;
                nextDirection = "up";
            }
            else if (nextDirection == "up")
            {
                for (int row = (size - 1) - bottom, col = (size - 1) - right; row >= top; row--)
                {
                    matrixD[row, col] = counter++;
                }

                right++;
                nextDirection = "left";
            }
            else if (nextDirection == "left")
            {
                for (int row = top, col = (size - 1) - right; col >= right; col--)
                {
                    matrixD[row, col] = counter++;
                }

                top++;
                nextDirection = "down";
            }
        }
    }

    private static void CalculateMatrixC(int size, int[,] matrixC)
    {
        int count = 1;

        // Filling the first diagonal half
        for (int i = 0; i < size; i++)
        {
            for (int k = 0, row = (size - 1) - i, col = 0; k <= i; k++)
            {
                matrixC[row++, col++] = count++;
            }
        }

        // Filling the second diagonal half
        for (int i = 1; i < size; i++)
        {
            for (int k = (size - 1) - i, row = 0, col = i; k >= 0; k--)
            {
                matrixC[row++, col++] = count++;
            }
        }
    }

    private static void CalculateMatrixB(int size, int[,] matrixB)
    {
        //Filling the even columns of the matrix
        for (int col = 0, element = 1; col < size; col += 2)
        {
            for (int row = 0; row < size; row++)
            {
                matrixB[row, col] = element++;
            }

            element += size;
        }

        //Filling the odd columns of the matrix
        for (int col = 1, element = 2 * size; col < size; col += 2)
        {
            for (int row = 0; row < size; row++)
            {
                matrixB[row, col] = element--;
            }

            element += 3 * size;
        }
    }

    private static void CalculateMatrixA(int size, int[,] matrixA)
    {
        // Filling the matrix
        for (int col = 0, element = 1; col < size; col++)
        {
            for (int row = 0; row < size; row++)
            {
                matrixA[row, col] = element++;
            }
        }
    }

    private static void PrintMatrix(int size, int[,] matrix)
    {
        for (int row = 0; row < size; row++)
        {
            for (int col = 0; col < size; col++)
            {
                Console.Write("{0,-3}", matrix[row, col]);
            }

            Console.WriteLine();
        }

        Console.WriteLine();
    }
}
